package com.masterlu.leetcode.daily.greedy.simple;

import java.util.Arrays;

/**
 * 1005. K 次取反后最大化的数组和
 * https://leetcode-cn.com/problems/maximize-sum-of-array-after-k-negations/
 *
 * @date created in 2021/6/8 8:52
 */
public class S1005LargestSumAfterKNegations {

    /**
     * 1.对原始数组进行初始排序
     * 2.循环K次，始终对数组最小值，进行取反操作
     */
    public int largestSumAfterKNegations(int[] nums, int k) {

        Arrays.sort(nums);
        for (int i = 0; i < k; i++) {
            nums[0] = nums[0] * -1;//始终使用最小值进行取反操作
            Arrays.sort(nums);
        }

        return Arrays.stream(nums).sum();
    }

}
